<template>
	<view class="month-calendar">
		<!-- 月视图 -->
		<swiper :current="currentMonthIndex" @change="handleMonthChange">
			<swiper-item v-for="(month, index) in months" :key="index">
				<view class="month-view">
					<view v-for="item in month.list" :key="item.date"
						:class="{ 'active': item.isSelected, 'today': item.isToday }" @click="selectMonth(item)">
						<view class="month">{{ item.title }}</view>
					</view>
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script setup>
	import {
		ref,
		computed,
		onMounted
	} from 'vue';

	// 当前月索引
	const currentMonthIndex = ref(0);

	// 生成月数据
	const months = ref([]);
	const generateMonths = () => {
		const today = new Date();
		const currentMonth = new Date(today.getFullYear(), today.getMonth(), 1); // 当前月
		const startMonth = new Date(currentMonth); // 起始月
		startMonth.setMonth(currentMonth.getMonth() - 3)
		months.value = Array.from({
			length: 5
		}, (_, i) => {
			const monthDate = new Date(startMonth);
			monthDate.setMonth(startMonth.getMonth() + i * 6); // 每增加一个月
			const list = Array.from({
				length: 6
			}, (_, j) => {
				const listStart = new Date(monthDate)
				listStart.setMonth(monthDate.getMonth() + j); // 每增加一个月
				const year = listStart.getFullYear();
				const month = listStart.getMonth() + 1; // 月份
				return {
					title: month === 1 ? `${year}.${month}` : month, // 月份标题
					date: listStart.toISOString().split('T')[0], // 日期（YYYY-MM-DD）
					isToday: currentMonth.toISOString().split('T')[0] === listStart.toISOString()
						.split('T')[0],
					isSelected: currentMonth.toISOString().split('T')[0] === listStart.toISOString()
						.split('T')[0]
				};
			});

			return {
				list
			}
		});
	};

	// 当前月标题
	const currentMonthRange = computed(() => {
		if (
			months.value.length > 0 &&
			currentMonthIndex.value >= 0 &&
			currentMonthIndex.value < months.value.length
		) {
			return months.value[currentMonthIndex.value].title;
		}
		return '加载中...';
	});

	// 切换月
	const handleMonthChange = (e) => {
		currentMonthIndex.value = e.detail.current;
	};
	// 选择日期
	const selectMonth = (selectedMonth) => {
		months.value = months.value.map((month) => {
			return {
				list: month.list.map((item) => {
					return {
						...item,
						isSelected: item.date === selectedMonth.date, // 选中当前日期
					};
				}),
			};
		});
		console.log('选中日期:', selectedMonth.date);
	};
	// 初始化生成月数据
	onMounted(() => {
		generateMonths();
	});
</script>
<style lang="scss" scoped>
	.month-calendar {
		height: 80rpx;
		padding: 20rpx;
	}

	.month-view {
		display: flex;
		justify-content: space-between;
		border-radius: 10rpx;
	}

	.month-view>view {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 10rpx;

		view {
			line-height: 48rpx;
			text-align: center;
			font-size: 28rpx;
			color: #A6A6A6;
			background: #F9F9F9;
			padding: 2rpx 18rpx;
			border: 1px solid #F9F9F9;
			border-radius: 30rpx;
		}
	}

	.month-view>view.today {
		.month {
			/* 当月样式 */
			border: 1px solid #FF9281;
			background-color: #fff;
		}
	}

	.month-view>view.active {
		.month {
			font-weight: 500;
			font-size: 28rpx;
			color: #fff;
			background: #FF9281;
			border: 1px solid #FF9281;
		}
	}
</style>